#/usr/bin/env bash
SCRIPT_BASE_DIR=$( dirname "${BASH_SOURCE[0]}" )
source "${SCRIPT_BASE_DIR}/common/fs_utils.sh"

set -e

function change_files_content() {
    local from=$1
    local to=$2
    # .py文件修改
    find . -name "*.py" |xargs sed -i "s/${from}/${to}/g"
    # .sh文件修改
    find . -name "*.sh" |xargs sed -i "s/${from}/${to}/g"
    # .md文件修改
    find . -name "*.md" |xargs sed -i "s/${from}/${to}/g"
}

function change_files_name() {
    local fPrefix=$1
    local tPrefix=$2
    local suffix=$3
    local a=($(find . -name ${fPrefix}\*.${suffix} -type f))
    # echo "${a[@]}"
    for oFile in "${a[@]}"
    do
        nFile=${oFile//${fPrefix}/${tPrefix}}
        #echo "${oFile} --> ${nFile}"
        mv -v ${oFile} ${nFile}
    done
}

function change_dirs_name() {
    local from=$1
    local to=$2
    local a=($(find . -name ${from} -type d))
    for oDir in "${a[@]}"
    do
        d=$( dirname ${oDir} )
        nDir="${d}/${to}"
        mv -v ${oDir} ${nDir}
    done
}

function change_file_content() {
    local fName=$1
    local from=$2
    local to=$3

    ps=($( find . -name ${fName} -type f))
    for p in "${ps[@]}" 
    do
        file_exists_or_die "${p}"
        # echo sed -i "s/${from}/${to}/g" "${p}"
        sed -i "s/${from}/${to}/g" "${p}"
    done
}

# 修改文件内容
echo "change file content tsm --> ts ..."
change_files_content tsm ts
echo "change file content Tsm... --> Ts..."
change_files_content Tsm Ts
echo "change file content TSM... --> TS..."
change_files_content TSM TS

# 修改文件名称
echo "change file name tsm_xxx --> ts_xxx"
# 参数: fromPrefix, toPrefix, suffix
change_files_name tsm_ ts_ py
change_files_name tsm_ ts_ md
change_files_name tsm- ts- sh
change_files_name config_tsm_ config_ts_ py
change_files_name config_tsm config_ts py
change_files_name TSM_ TS_ py
change_files_name TsmFbank TsFbank so

echo "change dir name tsm --> ts"
change_dirs_name tsm ts
